package com.liuhuanss.sheep.controller;

import com.liuhuanss.sheep.model.GameState;
import com.liuhuanss.sheep.service.GameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/api/game")
@CrossOrigin(origins = "*")
public class GameController {

    @Autowired
    private GameService gameService;

    @PostMapping("/new")
    public ResponseEntity<GameState> createNewGame() {
        String gameId = UUID.randomUUID().toString();
        GameState gameState = gameService.createNewGame(gameId);
        return ResponseEntity.ok(gameState);
    }

    @GetMapping("/{gameId}")
    public ResponseEntity<GameState> getGame(@PathVariable String gameId) {
        GameState gameState = gameService.getGame(gameId);
        if (gameState == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(gameState);
    }

    @PostMapping("/{gameId}/click")
    public ResponseEntity<GameState> clickBlock(
            @PathVariable String gameId,
            @RequestBody Map<String, String> request) {
        String blockId = request.get("blockId");
        GameState gameState = gameService.clickBlock(gameId, blockId);
        if (gameState == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(gameState);
    }
}